package cn.iocoder.yudao.module.planet.controller.app;

import cn.iocoder.yudao.module.planet.service.MembershipService;
import cn.iocoder.yudao.module.planet.controller.app.dto.MembershipDTO;
import cn.iocoder.yudao.module.planet.controller.app.dto.MembershipCreateReqDTO;
import cn.iocoder.yudao.module.planet.controller.app.dto.MembershipUpdateReqDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 成员关系管理控制器
 * 
 * @author 系统生成
 */
@RestController
@RequestMapping("/membership")
@Tag(name = "成员关系管理")
public class AppMembershipController {

    @Resource
    private MembershipService membershipService;

    @PostMapping
    @Operation(summary = "创建成员关系")
    public Long createMembership(@Valid @RequestBody MembershipCreateReqDTO createReqDTO) {
        return membershipService.createMembership(createReqDTO);
    }

    @PutMapping
    @Operation(summary = "更新成员关系")
    public void updateMembership(@Valid @RequestBody MembershipUpdateReqDTO updateReqDTO) {
        membershipService.updateMembership(updateReqDTO);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除成员关系")
    public void deleteMembership(@PathVariable Long id) {
        membershipService.deleteMembership(id);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取成员关系详情")
    public MembershipDTO getMembership(@PathVariable Long id) {
        return membershipService.getMembership(id);
    }

    @GetMapping("/list-by-user-and-planet")
    @Operation(summary = "根据用户ID和星球ID获取成员关系列表")
    public List<MembershipDTO> getMembershipListByUserIdAndPlanetId(
            @RequestParam Long userId, 
            @RequestParam Long planetId) {
        return membershipService.getMembershipListByUserIdAndPlanetId(userId, planetId);
    }

    @GetMapping("/list-by-user/{userId}")
    @Operation(summary = "根据用户ID获取其在所有星球中的成员关系")
    public List<MembershipDTO> getMembershipListByUserId(@PathVariable Long userId) {
        return membershipService.getMembershipListByUserId(userId);
    }

    @GetMapping("/list-by-planet/{planetId}")
    @Operation(summary = "根据星球ID获取成员关系列表")
    public List<MembershipDTO> getMembershipListByPlanetId(@PathVariable Long planetId) {
        return membershipService.getMembershipListByPlanetId(planetId);
    }

    @GetMapping("/has-permission")
    @Operation(summary = "检查用户是否有权限")
    public boolean hasPermission(
            @RequestParam Long userId, 
            @RequestParam Long planetId, 
            @RequestParam String type, 
            @RequestParam String permission) {
        return membershipService.hasPermission(userId, planetId, type, permission);
    }

    @GetMapping("/permissions")
    @Operation(summary = "获取用户在特定星球和分组类型中的所有权限")
    public Map<String, Boolean> getPermissions(
            @RequestParam Long userId, 
            @RequestParam Long planetId, 
            @RequestParam String type) {
        return membershipService.getPermissions(userId, planetId, type);
    }

}